<?php
namespace app\admin\model;
use think\Db;
/**
 * app基本信息管理
 * @author gaolei<1455209597@qq.com>
 */
class Push extends \think\Model{
    
    private $JG_key='326065a6711acf6960cfd731';
    private $JG_secret='bfb71c213407225aaf6649cc';
    private $JG_url='https://api.jpush.cn/v3/push';


    /**
     * 获取推送历史
     */
    public static function getPushMs($param=[],$order=''){
        return Db::name('push')->where($param)->order($order)->paginate();
    }
   /**
    * 删除推送
    */
    public static function deletePush($id){
        return Db::name('push')->where(['id'=>$id])->delete();
    }
   /**
    * 新增推送 
    */
    public static function addPush($param,$result=0){
        $insertData=[
            'title'=>$param['title'],
            'description'=>$param['description'],
            'url'=>$param['url'],
            'type'=>0,
            'status'=>$result,  
            'create_time'=> time(),
            'update_time'=> time(),
        ];
        $pushid=Db::name('push')->insertGetId($insertData);
        if($pushid){
            $push =new self();
            $res=$push->JGpush($param['title'],$param['description'],$param['url']);
            $reback=$res?json_decode($res,true):'';
            if(isset($reback['sendno'])&&$reback['sendno']==0){
                Db::name('push')->where(['id'=>$pushid])->update(['status'=>2]);
                return true;
            }else{
                Db::name('push')->where(['id'=>$pushid])->update(['status'=>1]);
                return  false;
            }
        }else{
            return false;
        }
    }
    /**
     * 极光推送核心方法
     */
    public function JGpush($description,$title,$url,$img='http://app.zcoming.com/static/images/cc.jpg'){
        $pushData=[
            'platform'=>'all',
            'audience'=>'all',
            'notification' => [
                'android' => [
                    'alert' => $title,
                    'title' => $description,
                    'large_icon' => $img,
                    'intent' => ['url' => $url]
                ],
                'ios'=>[
                    'alert' => $title,
                    'extras' => ['url' => $url]
                ],
            ],
            'message'=>[
                'msg_content'=>$description,
                'content_type'=>'text',
                'title'=>$title,
                'extras'=>[
                    'url'=>$url,  
                ],
            ],
        ];
        return $this->curlReq($pushData);
    }
    /**
     * 请求方法
     */
        /*
    * 公共请求方法改进版
    * @author 高磊
    */
   private function curlReq($data=''){
        //云信加密数据拼接
        $header=[];
        $header[]='Connection: Keep-Alive';
        $header[]="Content-Type:application/json"; 
        $header[]='Authorization:Basic '. base64_encode($this->JG_key.':'.$this->JG_secret);
        //获取云信链接
        $url= $this->JG_url;
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header); 
        curl_setopt($ch, CURLOPT_HEADER,0); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, $this->JG_key.':'.$this->JG_secret);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
        
        $res = curl_exec($ch); 
        curl_close($ch);
        return $res;
    }
}